import 'dart:ui';

import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/MarketService.dart';
import 'package:toast/toast.dart';

class Place extends StatefulWidget {
  @override
  _PlaceState createState() => _PlaceState();
}

class _PlaceState extends State<Place> {

  String city = null ;
  String name = null ;
  String phone = null ;
  String detail = null ;

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle> (
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          color: Color.fromARGB(255, 246, 246, 248),
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45,
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                      child: GestureDetector(
                        child: Container(
                          width: 17,
                          height: 17,
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Container(
                        child: Center(
                          child: Text("编辑收货地址" , style: TextStyle(
                            color: Color.fromARGB(255, 19 , 28 , 53) ,
                            fontSize: ScreenUtil().setSp(18) ,
                            fontWeight: FontWeight.bold 
                          ),),
                        ),
                      ),
                    ) ,
                    Container(
                      height: 17,
                      width: 36,
                      margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                      child: Text("删除" , textAlign: TextAlign.end, style: TextStyle(
                        fontSize: ScreenUtil().setSp(16) ,
                        color:Colors.red ,
                        height: 17 / ScreenUtil().setSp(16)
                      ),),
                    )
                  ],
                ),
              ) ,
              Expanded(
                child: ScrollConfiguration(
                  behavior: MyBehavior() ,
                  child: ListView(
                    padding: EdgeInsets.all(0),
                    children: <Widget>[
                      Container(
                        height: ScreenUtil().setWidth(301),
                        width: ScreenUtil().setWidth(345),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15) , top:ScreenUtil().setHeight(16)),
                        decoration: BoxDecoration(
                          color: Colors.white ,
                          borderRadius: BorderRadius.all(Radius.circular(8))
                        ),
                        child: Column(
                          children: <Widget>[
                            Container(
                              height: ScreenUtil().setWidth(59) ,
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                              child: Row(
                                children: <Widget>[
                                  Container(
                                    height: ScreenUtil().setWidth(20),
                                    width: ScreenUtil().setWidth(78),
                                    child: Text("收货人" , style: TextStyle(
                                      fontSize: ScreenUtil().setSp(14) ,
                                      color: Color.fromARGB(255, 78, 86, 108)
                                    ),),
                                  ) ,
                                  Expanded(
                                    child: Container(
                                      height: ScreenUtil().setWidth(59),
                                      // color: Colors.grey ,
                                      child: TextField(
                                        decoration: InputDecoration(
                                          hintText: "请输入收货人" ,
                                          border: InputBorder.none ,
                                          hintStyle: TextStyle(
                                            fontSize: ScreenUtil().setSp(14) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setWidth(59) / ScreenUtil().setSp(14) ,
                                          ) ,
                                        ),
                                        style: TextStyle(
                                          // color: Color.fromARGB(a, r, g, b)
                                        ),
                                        onChanged: (value){
                                          name = value ;
                                        },
                                      ),
                                    ),
                                  )
                                ],
                              ) ,
                            ) ,
                            Container(
                              height: ScreenUtil().setWidth(1),
                              margin:EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                              color: Color.fromARGB(255, 236, 237, 241),
                            ) ,
                            Container(
                              height: ScreenUtil().setWidth(59) ,
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                              child: Row(
                                children: <Widget>[
                                  Container(
                                    height: ScreenUtil().setWidth(20),
                                    width: ScreenUtil().setWidth(78),
                                    child: Text("手机号" , style: TextStyle(
                                      fontSize: ScreenUtil().setSp(14) ,
                                      color: Color.fromARGB(255, 78, 86, 108)
                                    ),),
                                  ) ,
                                  Expanded(
                                    child: Container(
                                      height: ScreenUtil().setWidth(59),
                                      // color: Colors.grey ,
                                      child: TextField(
                                        decoration: InputDecoration(
                                          hintText: "请输入手机号" ,
                                          border: InputBorder.none ,
                                          hintStyle: TextStyle(
                                            fontSize: ScreenUtil().setSp(14) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setWidth(59) / ScreenUtil().setSp(14) ,
                                          ) ,
                                        ),
                                        style: TextStyle(
                                          // color: Color.fromARGB(a, r, g, b)
                                        ),
                                        onChanged: (value){
                                          phone = value ;
                                        },
                                      ),
                                    ),
                                  )
                                ],
                              ) ,
                            ) ,
                            Container(
                              height: ScreenUtil().setWidth(1),
                              margin:EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                              color: Color.fromARGB(255, 236, 237, 241),
                            ) ,
                            GestureDetector(
                              child: Container(
                                height: ScreenUtil().setWidth(59) ,
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      height: ScreenUtil().setWidth(20),
                                      width: ScreenUtil().setWidth(78),
                                      child: Text("所在地区" , style: TextStyle(
                                        fontSize: ScreenUtil().setSp(14) ,
                                        color: Color.fromARGB(255, 78, 86, 108)
                                      ),),
                                    ) ,
                                    Expanded(
                                      child: Container(
                                        height: ScreenUtil().setWidth(59),
                                        child: Text(city == null ? "请选择所在区域" : city ,style: TextStyle(
                                          fontSize: ScreenUtil().setSp(14) ,
                                          color: Color.fromARGB(255, 121, 132, 155) ,
                                          height: ScreenUtil().setWidth(46) / ScreenUtil().setSp(14) ,
                                        ) ,),
                                      ),
                                    ) ,
                                    Container(
                                      width: ScreenUtil().setWidth(16),
                                      height: ScreenUtil().setWidth(16),
                                      child: Image.asset("assets/images/more_2x.png" , fit:BoxFit.fill ),
                                    )
                                  ],
                                ) ,
                              ),
                              onTap: () async{
                                Result result = await CityPickers.showCityPicker(
                                  context: context ,
                                  height: ScreenUtil().setHeight(224) ,
                                );
                                city = result.provinceName+"-"+result.cityName+"-"+result.areaName ;
                                setState(() {});
                              },
                            ) ,
                            Container(
                              height: ScreenUtil().setWidth(1),
                              margin:EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                              color: Color.fromARGB(255, 236, 237, 241),
                            ) ,
                            Expanded(
                              child: Container(
                                height: ScreenUtil().setWidth(121),
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(13) , right:ScreenUtil().setWidth(12)),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start ,
                                  children: <Widget>[
                                    Container(
                                      height: ScreenUtil().setWidth(59),
                                      width: ScreenUtil().setWidth(78),
                                      child: Text("详细地址" , style: TextStyle(
                                        fontSize: ScreenUtil().setSp(14) ,
                                        color: Color.fromARGB(255, 78, 86, 108) ,
                                        height: ScreenUtil().setWidth(46) / ScreenUtil().setSp(14)
                                      ),),
                                    ) ,
                                    Expanded(
                                      child: Container(
                                        height: ScreenUtil().setWidth(121),
                                        child: TextField(
                                          decoration: InputDecoration(
                                            hintText: "请输入详细地址" ,
                                            border: InputBorder.none ,
                                            hintStyle: TextStyle(
                                              fontSize: ScreenUtil().setSp(14) ,
                                              color: Color.fromARGB(255, 121, 132, 155) ,
                                              height: ScreenUtil().setWidth(34) / ScreenUtil().setSp(14) ,
                                            ) ,
                                          ),
                                          style: TextStyle(
                                            // color: Color.fromARGB(a, r, g, b)
                                          ),
                                          maxLines: 4,
                                          onChanged: (value){
                                            detail = value ;
                                          },
                                        ),
                                      ),
                                    )
                                  ],
                                ) ,
                              ) ,
                            )
                          ],
                        ),
                      ) ,
                      Container(
                        height: ScreenUtil().setHeight(53),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15) , top:ScreenUtil().setHeight(32)),
                        child: Stack(
                          children: <Widget>[
                            Positioned(
                              top:15 ,
                              left:44 ,
                              right:43 ,
                              bottom:0 ,
                              child: ClipRect(
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Color.fromARGB(30, 0, 89, 99) ,
                                  ),
                                  child: BackdropFilter(
                                    filter: ImageFilter.blur(
                                      sigmaX: 9 ,
                                      sigmaY: 9 
                                    ),
                                    child: Container(
                                      color: Colors.white.withOpacity(0.01) ,
                                    ),
                                  ),
                                ),
                              ),
                            ) ,
                            Positioned(
                              top:0 , 
                              left:0 , 
                              right: 0 ,
                              bottom: 5 ,
                              child: GestureDetector(
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 0, 195, 206) ,
                                    borderRadius: BorderRadius.all(Radius.circular(8)) ,
                                  ),
                                  child: Center(
                                    child: Text("保存" , style: TextStyle(
                                      color: Colors.white ,
                                      fontSize: ScreenUtil().setSp(18) ,
                                      fontWeight: FontWeight.bold 
                                    ),),
                                  ),
                                ),
                                onTap: (){
                                  FocusScope.of(context).requestFocus(FocusNode()) ;
                                  if(name == null || name == "" || phone == null || phone == "" || city == null || city == "" || detail == null || detail == ""){
                                    Toast.show("数据未填写完整", context) ;
                                    return ;
                                  }
                                  MarketService().addPlace(name, phone, city, detail).then((value){
                                    if(value["code"] == 200){
                                      Navigator.of(context).pop(value["data"]) ;
                                    }else{
                                      Toast.show(value["msg"], context) ;
                                    }
                                  }) ;
                                },
                              ),
                            ) ,
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}